<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- this stylesheet extracts the response data of a webrowset 
	and converts the content to a XML-toolbox readable format -->
<xsl:stylesheet version="1.0"
	xmlns:ns1="http://ogsadai.org.uk/namespaces/2005/10/types"
	xmlns:ns2="http://java.sun.com/xml/ns/jdbc"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	exclude-result-prefixes="ns1 ns2">
	<xsl:output method="xml" version="1.0" encoding="iso-8859-1"
		indent="yes" />

	<!-- convert a webrowset to the octave typesafe nested cell representation -->
	<xsl:template match="/ns2:webRowSet">
		<xsl:variable name="rows"
			select="count(ns2:data/ns2:currentRow)" />
		<xsl:variable name="cols"
			select="ns2:metadata/ns2:column-count" />

		<xsl:choose>
			<!-- when rs contains some rows, return cell-->
			<xsl:when test="$rows != 0">
				<root>
					<xsl:attribute name="idx">
				<xsl:text>1</xsl:text>
			</xsl:attribute>
					<xsl:attribute name="type">
				<xsl:text>cell</xsl:text>
			</xsl:attribute>
					<xsl:attribute name="size">
				<xsl:text>1 </xsl:text>
				<xsl:value-of select="$rows" />
			</xsl:attribute>

					<xsl:for-each select="ns2:data/ns2:currentRow">
						<item>
							<xsl:attribute name="idx">
						<xsl:value-of select="position()" />
					</xsl:attribute>
							<xsl:attribute name="type">
						<xsl:text>cell</xsl:text>
					</xsl:attribute>
							<xsl:attribute name="size">
						<xsl:text>1 </xsl:text><xsl:value-of
									select="$cols" />
					</xsl:attribute>
							<xsl:for-each select="ns2:columnValue">
								<item>
									<xsl:attribute name="idx">
								<xsl:value-of select="position()" />
							</xsl:attribute>
									<xsl:attribute name="type">
								<xsl:call-template
											name="lookup_elem_type">
    								<xsl:with-param name="index"
												select="position()" />
  								</xsl:call-template>
							</xsl:attribute>
									<xsl:attribute name="size">
								<xsl:text>1 </xsl:text>
								<xsl:value-of select="string-length(.)" />
							</xsl:attribute>
									<xsl:value-of select="." />
								</item>
							</xsl:for-each>
						</item>
					</xsl:for-each>
				</root>
			</xsl:when>
			<xsl:otherwise>
				<!-- else return a negative value -->
				<root>
					<xsl:attribute name="idx">
				<xsl:text>1</xsl:text>
			</xsl:attribute>
					<xsl:attribute name="type">
				<xsl:text>double</xsl:text>
			</xsl:attribute>
					<xsl:attribute name="size">
				<xsl:text>1 1</xsl:text>
			</xsl:attribute>
					<xsl:text>-1</xsl:text>
				</root>
			</xsl:otherwise>
		</xsl:choose>


	</xsl:template>

	<!-- lookup datatype for a column, by the given col-index
		yes, probably this could be done using a cache but for 
		now this quick hack does what we want
	-->
	<xsl:template name="lookup_elem_type">
		<xsl:param name="index" />
		<xsl:variable name="datatype"
			select="/ns2:webRowSet/ns2:metadata/ns2:column-definition[number($index)]/ns2:column-type-name" />
		<xsl:if test="contains($datatype, 'CHAR')">
			<xsl:text>char</xsl:text>
		</xsl:if>
		<xsl:if test="contains($datatype, 'INT')">
			<xsl:text>double</xsl:text>
		</xsl:if>
	</xsl:template>

	<!-- OGSA-DAI UPDATE response -->
	<xsl:template match="/resultStream">
		<root>
			<xsl:attribute name="idx">
				<xsl:text>1</xsl:text>
			</xsl:attribute>
			<xsl:attribute name="type">
				<xsl:text>double</xsl:text>
			</xsl:attribute>
			<xsl:attribute name="size">
				<xsl:text>1 1</xsl:text>
			</xsl:attribute>
			<xsl:value-of select="@updateCount" />
		</root>
	</xsl:template>

</xsl:stylesheet>